<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
String areaId = request.getParameter("areaId");
request.setAttribute("areaId",areaId); 
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
	body,html{
		margin: 0 auto;
		height:100%;
		width:100%;
	}
</style>
</head>
<body>

<div id="system_areaAddMain" style="width:100%;height:100%;overflow: auto;" class="divfrist">
			<DIV id="" style="width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" style="width:100%;height:98.6%;">
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:50%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;overflow:auto;"  >
							   <!--内容区 -->
												<div style="padding:10px 0 10px 60px">
															    <form id="system_areaAddff" method="post">
															        <input type="hidden" name="areaId" id="areaId">
															        <input type="hidden" name="areaTID" id="areaTID">
															        <input type="hidden" name="areaPID" id="areaPID">
															    	<table>
															    		<tr>
															    	 		<td>区域名称:</td>
															    			<td><input  type="text" name="areaName" id="areaName"></input></td>
															    		</tr>
															    		<tr>
															    			<td>上级区域:</td>
															    			<td><input type="text" name="areaPName" id="areaPName" disabled="disabled"></textarea></td>
															    		</tr>
																		<tr>
															    			<td>区域邮编:</td>
															    			<td><input    name="areaPost" id="areaPost"></input></td>
															    		</tr>
															    		<tr>
															    			<td>区域描述:</td>
															    			<td><textarea    name="areaNote" id="areaNote"></textarea></td>
															    		</tr>
															    										    		
															    	</table>
															    </form>
															   </div>
															    <div style="text-align:center;padding:5px">
															    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="window.areaAdd.addarea()">确认</a>
															    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="window.areaAdd.clearForm()">清空</a>
															    </div>	
							   <!--内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:48.8%;height:100%;">
							<DIV id=""class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;margin-left:-1px;" >
								<!-- 内容区 -->
											<ul id="system_areaAddTree" class="ztree" style="margin-top:0; width:160px;"></ul>
								<!-- 内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.37%;height:100%;overflow:hidden;"></DIV>
			</DIV>
			<DIV id="" class="div_bottom"></DIV>
	</div>
<script type="text/javascript">
(function(){
	//增加区域
	function addarea(){
		var zTree = $.fn.zTree.getZTreeObj("system_areaAddTree");
		nodes = zTree.getSelectedNodes();
		if(nodes&&nodes.length>0){
			if($("#areaTID")==$("#areaPID").val()){
				alert("不能设置自己为自己的上级区域!请选择其他区域");
				return;
			}
		}
		$.messager.progress({
			title:'请注意',
			text:'数据正在加载.....'
		});
		$('#system_areaAddff').form('submit', {
				url: "${pageContext.request.contextPath}/sys/sysAreaAction!areaAdd.action",
				onSubmit: function(){
				var isValid = $(this).form('validate');
				if (!isValid){
					$.messager.progress('close');	// 当form不合法的时候隐藏工具条
				}
				return isValid;	// 返回false将停止form提交 
			},
			success: function(data){
				$.messager.progress('close');
				if(data=="success"){
					alert("增加区域成功");
					createUpdateTree();
				}else if(data=="same"){
					alert("已经有相同名称的区域存在");
				}else if(data=="us"){
					alert("更新成功");
				}else{
					alert("操作失败");
				}
				// 当成功提交之后隐藏进度条
			}
		});
	}
	//双击树改变区域选择
	function onClick(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("system_areaAddTree");
		nodes = zTree.getSelectedNodes();
		$("#areaPName").val(nodes[0].name);
		$("#areaPID").val(nodes[0].id);
		
	}
	//清空
	function clearForm(){
		
	}
	window["areaAdd"]={};
	window["areaAdd"]["addarea"]=addarea;
	window["areaAdd"]["clearForm"]=clearForm;
	function createUpdateTree(){
		
		var setting = {
				view: {
					dblClickExpand: false
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: onClick
				}
			};
		$.ajax({
			   type: "POST",
			   url: "${pageContext.request.contextPath}/sys/sysAreaAction!getareaOutSelf.action",
			   data:{areaId:"${areaId}"},
			   dataType:'json',
			   success: function(msg){
				   try{
					   var node = new Object();
					   node.uuid=0;
					   node.id=1;
					   node.pid=0;
					   node.name="根区域";
					   node.open=true;
					   msg.push(node);
					   $.fn.zTree.init($("#system_areaAddTree"), setting, msg);  	   
					   
				   }catch(e){
					   alert(e);
				   }
				 
			   }
		});	
	}
	//加载初始化
	$(function(){	
		$.parser.parse('#system_areaAddMain'); 
		var treeObj = $.fn.zTree.getZTreeObj("addarea_tree");
		var nodes = treeObj.getSelectedNodes();
		var valuetype=["length[2,15]","unnormal","same['sysIO_pwd']"];
		$('#areaName').validatebox({   
		    required: true,   
		    validType: ["length[1,15]","unnormal"] 
		});
		$('#areaPost').validatebox({   
		    required: true,   
		    validType: ["unnormal","length[1,15]","integer"] 
		}); 
		$('#areaNote').validatebox({   
		    required: true,   
		    validType: ["unnormal","length[1,15]"] 
		}); 
		createUpdateTree();
		//如果有id表示为更新
		if("${areaId}"!=null&&"${areaId}"!=""){
			//加载修改数据
			
			$.ajax({
			   type: "POST",
			   async:false,
			   url: "${pageContext.request.contextPath}/sys/sysAreaAction!getarea.action",
			   data:{areaId:"${areaId}"},
			   dataType:'text',
			   success: function(msg){	
				   var temp = eval('('+msg+')');				 
				   $('#system_areaAddff').form('load',temp);
				   var node =nodes[0].getParentNode();
				   $("#areaPName").val(node.name);
				 
			   },
			   error:function(a,b,c){
				   alert("查询角色失败");
				}
			});
			
		}else{
			if(nodes.length>0){
				$("#areaPName").val(nodes[0].name);
				$("#areaPID").val(nodes[0].id);
			}
		}
	});
})();
</script>
</body>
</html>